package graduate.cslg.com.universityshopping.mainPage;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.BBSItemAdapter;
import graduate.cslg.com.universityshopping.entity.PostListBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TransactionActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private RecyclerView mRecyclerView;
    private List<PostListBean> mPostListBeans;
    private BBSItemAdapter mBBSItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);

        mToolbar = findViewById(R.id.toolbar);
        mRecyclerView = findViewById(R.id.transaction_recycler);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getTransInfo();
    }

    private void getTransInfo() {
        RetrofitDevelop.getTestService()
                .getTransInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PostListBean>>() {
                    @Override
                    public void onCompleted() {
                        mBBSItemAdapter = new BBSItemAdapter(mPostListBeans);
                        mRecyclerView.setAdapter(mBBSItemAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<PostListBean> beanList) {
                        mPostListBeans = beanList;
                    }
                });
    }
}
